package org.jeecg.modules.api.home;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Month;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.ijpay.core.kit.IpKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.config.app.WechatUtil;
import org.jeecg.config.vo.WxPayVo;
import org.jeecg.modules.api.commissionlog.mapper.CommissionLogMapper;
import org.jeecg.modules.api.member.mapper.MemberMapper;
import org.jeecg.modules.api.order.mapper.OrderMapper;
import org.jeecg.modules.api.withdraw.entity.WithdrawRecord;
import org.jeecg.modules.api.withdraw.mapper.WithdrawRecordMapper;
import org.jeecg.modules.api.withdraw.service.IWithdrawRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 首页统计
 * @Author: jeecg-boot
 * @Date: 2024-10-21
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "首页统计")
@RestController
@RequestMapping("/home")
public class HomeController extends JeecgController<WithdrawRecord, IWithdrawRecordService> {
    @Autowired
    private IWithdrawRecordService withdrawRecordService;
    @Autowired
    private MemberMapper memberMapper;
    @Value("${sys.online}")
    private Boolean online;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommissionLogMapper commissionLogMapper;
    @Autowired
    private WithdrawRecordMapper withdrawRecordMapper;

    /**
     * 首页统计
     */
    @ApiOperation(value = "首页统计", notes = "首页统计")
    @GetMapping(value = "/statistics")
    public Result<?> statistics(
            HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();
        result.put("userTotal", memberMapper.count());//总用户数量
        Map<String, BigDecimal> commissionAmountMap = memberMapper.getCommissionAmount();
        BigDecimal totalAmount = commissionAmountMap.get("commission");
        BigDecimal remainAmount = commissionAmountMap.get("withdrawMoney");
        BigDecimal hasWithdrawAmount = totalAmount.subtract(remainAmount);
        result.put("totalAmount", totalAmount);//累计奖励金额
        result.put("withdrawAmount", hasWithdrawAmount);//已提现金额
        result.put("remainAmount", remainAmount);//未提现金额
        result.put("monthMoney", this.getMonthMoneyStatice());//每月推荐奖励了多少钱，  提现了多少钱（趋势图）
        result.put("monthOrder", this.getMonthUserStatice());//每月推荐了多少人，  实际通过推荐人消费了多少人（趋势图）
        return Result.OK(result);
    }

    //每月推荐了多少人，  实际通过推荐人消费了多少人
    private Map<String, Map<String, Integer>> getMonthUserStatice() {
        //每月推荐了多少人，  实际通过推荐人消费了多少人
        Map<String, Map<String, Integer>> newUserGroupByMonth = Maps.newLinkedHashMap();
        Date now = new Date();
        for (int i = 11; i >= 0; i--) {
            Date date = DateUtil.offsetMonth(now, -i);
            Month month = DateUtil.monthEnum(date);
            String time = DateUtils.formatDate(date, "yyyy-MM");

            int recCount = memberMapper.sumRecCount(time);
            int orderUserCount = orderMapper.sumOrderUserCount(time);
            Map<String, Integer> result = new HashMap<>();
            result.put("recCount", recCount);//推荐人数
            result.put("orderCount", orderUserCount);//下单消费人数
            newUserGroupByMonth.put((month.getValue() + 1) + "月", result);
        }
        return newUserGroupByMonth;
    }

    //每月推荐奖励了多少钱，  提现了多少钱
    private Map<String, Map<String, BigDecimal>> getMonthMoneyStatice() {
        Map<String, Map<String, BigDecimal>> newGroupByMonth = Maps.newLinkedHashMap();
        Date now = new Date();
        for (int i = 11; i >= 0; i--) {
            Date date = DateUtil.offsetMonth(now, -i);
            Month month = DateUtil.monthEnum(date);
            String time = DateUtils.formatDate(date, "yyyy-MM");
            BigDecimal recMoney = commissionLogMapper.getSumFyMoneyAmount(time);
            BigDecimal withdrawMoney = withdrawRecordMapper.getSumMoneyAmount(time);
            Map<String, BigDecimal> result = new HashMap<>();
            result.put("recMoney", recMoney);//推荐奖励多少钱
            result.put("withdrawMoney", withdrawMoney);//提现多少钱
            newGroupByMonth.put((month.getValue() + 1) + "月", result);
        }
        return newGroupByMonth;
    }


}
